package com.ehouse.database;

import java.util.Collection;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import com.ehouse.models.Rule;
import com.ehouse.provider.MainContentProvider;


public class IrrigationScheduleTable {
//	// Database table
		public static final String NAME = "irrigation_schedules";
		public static final String COLUMN_ID = "_id";
		public static final String COLUMN_OBJ_ID = "objId";
		public static final String COLUMN_STATE = "state";
		public static final String COLUMN_TIME = "time";

		public static final Uri CONTENT_URI = Uri.parse(MainContentProvider.BASE_URI.toString() + NAME);
		
		// Database creation SQL statement
		public static final String DATABASE_CREATE = "create table " 
				+ NAME
				+ "(" + COLUMN_ID + " integer primary key autoincrement, " 
				+ COLUMN_OBJ_ID + " text  not null, " 
				+ COLUMN_TIME + " text not null, "
				+ COLUMN_STATE + " integer not null " + ");";

		public static void update(ContentResolver cr, Collection<Rule> rules){
			String[] dprojection = new String[]{IrrigationScheduleTable.COLUMN_ID, IrrigationScheduleTable.COLUMN_OBJ_ID, IrrigationScheduleTable.COLUMN_STATE, IrrigationScheduleTable.COLUMN_TIME};
	    	Cursor dquery = cr.query(IrrigationScheduleTable.CONTENT_URI, dprojection, null, null, null);
	    	if(dquery.getCount() !=0){
		   		   cr.delete(IrrigationScheduleTable.CONTENT_URI, null, null);
		   	   }
	    	dquery.close();
	    	
			for (Rule rule : rules) {
				String where = IrrigationScheduleTable.COLUMN_OBJ_ID+" = '"+rule.getId()+"'";
		    	String[] projection = new String[]{IrrigationScheduleTable.COLUMN_ID, IrrigationScheduleTable.COLUMN_OBJ_ID, IrrigationScheduleTable.COLUMN_STATE, IrrigationScheduleTable.COLUMN_TIME};
		    	Cursor query = cr.query(IrrigationScheduleTable.CONTENT_URI, projection, where, null, null);
		    	
	  	       ContentValues values = new ContentValues();

	   	       values.put(IrrigationScheduleTable.COLUMN_OBJ_ID, rule.getId());
	   	       values.put(IrrigationScheduleTable.COLUMN_TIME, rule.getTime());
	   	       values.put(IrrigationScheduleTable.COLUMN_STATE, (Boolean)rule.getData());
	   	       
		   	   if(query.getCount() ==0){
		   	      cr.insert(IrrigationScheduleTable.CONTENT_URI, values);   	
		   	   }else{
		   		   cr.update(IrrigationScheduleTable.CONTENT_URI, values, where, null);
		   	   }
		   	query.close();
		  }
	}
		
		public static void delete(ContentResolver cr, String ObjId){
			String where = IrrigationScheduleTable.COLUMN_OBJ_ID+" = '"+ObjId+"'";
			String[] projection = new String[]{IrrigationScheduleTable.COLUMN_ID, IrrigationScheduleTable.COLUMN_OBJ_ID, IrrigationScheduleTable.COLUMN_STATE, IrrigationScheduleTable.COLUMN_TIME};
	    	Cursor query = cr.query(IrrigationScheduleTable.CONTENT_URI, projection, where, null, null);
	    	if(query.getCount() !=0){
		   		   cr.delete(IrrigationScheduleTable.CONTENT_URI, where, null);
		   	   }
	    	query.close();
		}
}
